﻿/*标记地图模块化*/
/*公交*/

//下面设置显示/隐藏层控制
//显示全部详情
$(document).on("click", "#puckerAll", function () {
    if ($(this).hasClass("packup-all")) {
        $(this).removeClass("packup-all").addClass("sr-on");
        $("div.bus_contain>dl>dt>span.packup").text("收起");
        $("div.bus_contain>dl>dd").show();
    }
    else {
        $(this).removeClass("sr-on").addClass("packup-all");
        $("div.bus_contain>dl>dt>span.packup").text("展开");
        $("div.bus_contain>dl>dd").hide();
    }
});

//展开/收起
$(document).on("click", "div.bus_contain>dl>dt>span.packup", function () {
    if ($(this).text() == "收起") {
        $(this).text("展开");
        $(this).parent("dt").next("dd").hide();
    }
    else if ($(this).text() == "展开") {
        $(this).text("收起");
        $(this).parent("dt").next("dd").show();
    }
});

var ISS_Map_bus = {
    //公交线路查询
    BusLine: function (station, station2, startname, endname, policy) {
        var policy_enum = 0;
        //最小时间
        if (policy == "BMAP_TRANSIT_POLICY_LEAST_TIME") {
            policy_enum = 1;
        }
        //最小换乘
        else if (policy == "BMAP_TRANSIT_POLICY_LEAST_TRANSFER") {
            policy_enum = 2;
        }
        //最少步行
        else if (policy == "BMAP_TRANSIT_POLICY_LEAST_WALKING") {
            policy_enum = 3;
        }
        //公交查询
        this.selectMapPoint = function () {
            var transit = new BMap.TransitRoute(ISS_MAP.nearByMap, { renderOptions: { map: ISS_MAP.nearByMap, panel: "r-result1" }, policy: 0 });
            var searchResult = true;
            //添加查询排序：最少时间、最少换乘、最少步行
            transit.setPolicy(policy_enum);
            transit.search(station, station2);

            jQuery("#pickresult").html('');  //先清空历史记录
            ISS_MAP.ClearArc();  //清除所有覆盖物

            transit.setSearchCompleteCallback(function (results) {

                if (transit.getStatus() == BMAP_STATUS_SUCCESS) {
                    policy = "BMAP_TRANSIT_POLICY_LEAST_TIME";

                    switch (policy) {
                        case "BMAP_TRANSIT_POLICY_LEAST_TIME":
                            {
                                //最少时间
                                var t_result = [];
                                if (results.getNumPlans() > 0) {
                                    //展示
                                    var plan = results.getPlan(0);
                                    t_result.push("<div class=\"search-info\">约" + plan.getDistance(true) + "/" + plan.getDuration(true) + "</div>");
                                    t_result.push("<div class=\"search-result\" id=\"littleTime_Result\">");
                                    t_result.push("<h5><a href=\"#\"><p><i class=\"ico-start\"></i>起点</p></a><span id=\"puckerAll\" class=\"packup-all\">显示全部详情</span></h5>");
                                    t_result.push("<div class='bus_contain'></div>");
                                    t_result.push("<h5><a href=\"#\"><p><i class=\"ico-end\"></i>终点</p></a></h5>");
                                    jQuery("#pickresult").html(t_result.join(""));
                                    for (var index = 0; index < results.getNumPlans(); index++) {
                                        var showElement = jQuery("<dl></dl>");
                                        //标题要用类似 10路->2路 样式
                                        var lineTitle = "";
                                        for (var indexLine = 0; indexLine < results.getPlan(index).getNumLines(); indexLine++) {
                                            if (indexLine != results.getPlan(index).getNumLines() - 1) {
                                                lineTitle += results.getPlan(index).getLine(indexLine).title + "->";
                                            } else {
                                                lineTitle += results.getPlan(index).getLine(indexLine).title;
                                            }
                                        }
                                        //改为lineTitle
                                        jQuery(showElement).html("<dt><i>" + parseInt(index + 1) + ". " + lineTitle + " </i> <span class=\"packup\">展开</span></dt><dd>" + results.getPlan(index).getDescription() + "</dd>")
                                        jQuery(".bus_contain").append(showElement);
                                        jQuery(".bus_contain dl").on("click", function () {
                                            var _index = jQuery(".bus_contain dl").index(this);
                                            ISS_Map_bus.DrawLine(map, results, _index, showElement);
                                            jQuery(this).css("background-color", "rgb(240, 240, 240)").siblings().css("background-color", "");
                                        })
                                    }
                                } else {
                                    jQuery("#pickresult").css("display", "block");
                                    jQuery("#pickresult").html("<div class=\'search-info\'>未找到相关公交站点信息</div>"); //步行查询提示
                                    searchResult = false;
                                }
                                break;
                            }
                    }
                }
                else {
                    jQuery("#pickresult").css("display", "block");
                    jQuery("#pickresult").html("<div class=\'search-info\'>未找到相关公交站点信息</div>"); //步行查询提示
                    searchResult = false;
                }
                //显示结果
                setTimeout(
                    function () { ISS_MAP.PickModal(searchResult); },
                    1000);
            });
        }
    },
    DrawLine: function (aMap, results, index, obj) {
        var opacity = 0.45;
        var planObj = results.getPlan(index);
        var bounds = new Array();
        var addMarkerFun = function (point, imgType, index, title) {
            var url;
            var width;
            var height
            var myIcon;
            // imgType:1的场合，为起点和终点的图；2的场合为过程的图形
            if (imgType == 1) {
                url = "http://map.baidu.com/image/dest_markers.png";
                width = 42;
                height = 34;
                myIcon = new BMap.Icon(url, new BMap.Size(width, height), { offset: new BMap.Size(14, 32), imageOffset: new BMap.Size(0, 0 - index * height) });
            } else {
                url = "http://map.baidu.com/image/trans_icons.png";
                width = 22;
                height = 25;
                var d = 25;
                var cha = 0;
                var jia = 0
                if (index == 2) {
                    d = 21;
                    cha = 5;
                    jia = 1;
                }
                myIcon = new BMap.Icon(url, new BMap.Size(width, d), { offset: new BMap.Size(10, (11 + jia)), imageOffset: new BMap.Size(0, 0 - index * height - cha) });
            }

            var marker = new BMap.Marker(point, { icon: myIcon });
            if (title != null && title != "") {
                marker.setTitle(title);
            }
            // 起点和终点放在最上面
            if (imgType == 1) {
                marker.setTop(true);
            }
            aMap.addOverlay(marker);
        } //end addMarkerFun
        var addPoints = function (points) {
            for (var i = 0; i < points.length; i++) {
                bounds.push(points[i]);
            }

        }

        // 清空覆盖物
        aMap.clearOverlays();

        // 绘制驾车步行线路
        for (var i = 0; i < planObj.getNumRoutes(); i++) {
            var route = planObj.getRoute(i);
            if (route.getDistance(false) > 0) {
                // 步行线路有可能为0
                aMap.addOverlay(new BMap.Polyline(route.getPath(), { strokeStyle: "dashed", strokeColor: "#30a208", strokeOpacity: 0.75, strokeWeight: 4, enableMassClear: true }));
            }
        }
        // 绘制公交线路
        for (i = 0; i < planObj.getNumLines(); i++) {
            var line = planObj.getLine(i);
            addPoints(line.getPath());
            // 公交
            if (line.type == BMAP_LINE_TYPE_BUS) {
                // 上车
                addMarkerFun(line.getGetOnStop().point, 2, 2, line.getGetOnStop().title);
                // 下车
                addMarkerFun(line.getGetOffStop().point, 2, 2, line.getGetOffStop().title);

                // 地铁
            } else if (line.type == BMAP_LINE_TYPE_SUBWAY) {
                // 上车
                addMarkerFun(line.getGetOnStop().point, 2, 3, line.getGetOnStop().title);
                // 下车
                addMarkerFun(line.getGetOffStop().point, 2, 3, line.getGetOffStop().title);
            }
            aMap.addOverlay(new BMap.Polyline(line.getPath(), { strokeColor: "#0030ff", strokeOpacity: opacity, strokeWeight: 6, enableMassClear: true }));
        }

        aMap.setViewport(bounds);
        // 终点
        addMarkerFun(results.getEnd().point, 1, 1);
        // 开始点
        addMarkerFun(results.getStart().point, 1, 0);

        // 设置文字背景色
        if (ISS_Global_Variable.lastSetBackObj != null) {
            ISS_Global_Variable.lastSetBackObj.css("background-color", "#FFFFFF");
        }
        obj.css("background-color", "#f0f0f0");
        ISS_Global_Variable.lastSetBackObj = obj;
    },
    //公交搜索结果tab切换
    SearchBusTab: function (id) {
        var bus_scheme = "BMAP_TRANSIT_POLICY_LEAST_TIME";
        switch (id) {
            case 0:
                bus_scheme = "BMAP_TRANSIT_POLICY_LEAST_TIME";
                break;
            case 1:
                bus_scheme = "BMAP_TRANSIT_POLICY_LEAST_TRANSFER";
                break;
            case 2:
                bus_scheme = "BMAP_TRANSIT_POLICY_LEAST_WALKING";
                break;
            default:
                bus_scheme = "BMAP_TRANSIT_POLICY_LEAST_TIME";
                break;
        }
        //定义起始点
        var startpoint;
        var endpoint;
        if (ISS_MAP_SEARCH_START.Lng && ISS_MAP_SEARCH_START.Lng != ''
        && ISS_MAP_SEARCH_START.Lat && ISS_MAP_SEARCH_START.Lat != '') {
            startpoint = new BMap.Point(ISS_MAP_SEARCH_START.Lng, ISS_MAP_SEARCH_START.Lat);
        } else {
            startpoint = ISS_MAP_SEARCH_START.Name;
        }
        if (ISS_MAP_SEARCH_END.Lng && ISS_MAP_SEARCH_END.Lng != ''
        && ISS_MAP_SEARCH_END.Lat && ISS_MAP_SEARCH_END.Lat != '') {
            endpoint = new BMap.Point(ISS_MAP_SEARCH_END.Lng, ISS_MAP_SEARCH_END.Lat);
        } else {
            endpoint = ISS_MAP_SEARCH_END.Name;
        }
        var busline = new ISS_Map_bus.BusLine(startpoint, endpoint, ISS_MAP_SEARCH_START.Name, ISS_MAP_SEARCH_END.Name, bus_scheme);
        busline.selectMapPoint();
    }
}